<template>
	<view>
		<u-cell-group title="好友申请" v-if="applyList.length>0">
			<template v-if="applyList.length>0">
				<u-cell-item v-for="(item,index) in applyList" :key="index" :arrow="false" hover-class="none">
					<view slot="icon" class="u-m-r-20">
						<static-image :src="item.to_user.avatar"  mode="aspectFill" :width="80" :height="80" :radius="12"
              v-if="item.from_user_id === vuex_userInfo.id"
              @on-tap="$u.route({url: '/pages/chat/friends/friendHome', params: {user_id: item.to_user.id}})"
            >
						</static-image>
            <static-image :src="item.from_user.avatar"  mode="aspectFill" :width="80" :height="80" :radius="12"
              v-else
              @on-tap="$u.route({url: '/pages/chat/friends/friendHome', params: {user_id: item.from_user.id}})"
            >
						</static-image>
					</view>
					<view slot="title">
            <view class="a">{{item.from_user_id === vuex_userInfo.id ? item.to_user.name : item.from_user.name}}</view>
            <view>{{ item.apply_remark }}</view>
          </view>
          <view slot="right-icon" v-if="item.from_user_id === vuex_userInfo.id">
						<view class="u-flex u-row-between u-col-center" v-if="item.status === 'PENDDING'">
							申请中
						</view>
						<view class="u-flex u-row-between u-col-center" v-if="item.status === 'REJECT'">
              已拒绝
						</view>
					</view>
					<view slot="right-icon" v-else>
						<view class="u-flex u-row-between u-col-center" v-if="item.status === 'PENDDING'">
							<u-button    @click="handleApply(item, 'OK')" size="mini" type="success" shape="square">同意</u-button>
							<view class="u-m-l-10 u-m-r-10"></view>
							<u-button    @click="handleApply(item, 'REJECT')" size="mini" type="error" shape="square">拒绝</u-button>
						</view>
						<view class="u-flex u-row-between u-col-center" v-if="item.status === 'REJECT'">
              已拒绝
						</view>
					</view>
				</u-cell-item>
			</template>
		</u-cell-group>
		<!-- <u-cell-group title="历史记录"></u-cell-group> -->
		<view  v-if="applyList.length<1" class="u-flex u-row-center u-col-center" style="min-height: 600rpx;">
			<u-empty text="暂无申请好友信息"></u-empty>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				applyList:[]
			};
		},
		onShow(option){
      uni.setStorageSync('contactList_refresh', true)
			this.checkFriendApply();
		},
		onNavigationBarButtonTap:function(e){
			if(e.index==0){
				this.$u.route({
					url: '/pages/chat/friends/addFriend',
				})
			}
		},
		methods:{
			//检查是否有人添加我为好友  2022年1月17日21:21:13
			async checkFriendApply(){
				const res = await this.$u.api.imUser.getApplyList({})
        // if (res.code !== 200 || res.data.length === 0) {
        //   uni.removeTabBarBadge({
        //     index:1
        //   })
        // } else {
        //   uni.setTabBarBadge({
        //     index: 1,
        //     text: res.data.length.toString()
        //   })
        // }
        this.applyList = res.data
			},
			
			async handleApply(item, status){
				await this.$u.api.imUser.handleFriendApply({
          apply_id:item.id,
					status
        })	
        this.checkFriendApply()
			}
		}
	}
</script>

<style lang="scss">

</style>
